package com.test;

public class AddToBox {

	private static int MAX_NUM = 5;

	private static Integer BoxNowNum = 0;

	static class AddBox  implements Runnable {

		@Override
		public void run() {
			if (BoxNowNum < MAX_NUM) {
				BoxNowNum++;
			}
			System.out.println("add"+BoxNowNum);
		}
	}


	static class ReduceFromBox implements Runnable {

		@Override
		public void run() {
			if(BoxNowNum>0){
				BoxNowNum--;
			}
			System.out.println("red"+BoxNowNum);
		}

	}

	public static void main(String[] args) {
		AddBox AddBox = new AddBox();
		//Thread t1 = new Thread(AddToBox);

		ReduceFromBox ReduceFromBox = new ReduceFromBox();
		//Thread t2 = new Thread(ReduceFromBox);
		while(true){
			Thread t1 = new Thread(AddBox);
			t1.start();
			Thread t2 = new Thread(ReduceFromBox);
			t2.start();
			System.out.println(BoxNowNum);
		}


	}

}
